package org.example.web.response;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 响应字符数据
 */
@WebServlet("/httpServletDemo3")
public class HttpServletDemo3 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置content-type，顺便解决中文乱码问题
        //resp.setHeader("content-type","text/html");
        resp.setContentType("text/html;charset=utf-8");
        //获取字符输出流
        PrintWriter writer = resp.getWriter();

        writer.write("<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>Title</title>\n" +
                "</head>\n" +
                "<body>\n" +
                "<form action=\"#\" method=\"get\">\n" +
                "    <input type=\"text\" name=\"username\"><br>\n" +
                "    <input type=\"password\" name=\"password\"><br>\n" +
                "    <input type=\"checkbox\" name=\"hobby\" value=\"1\"> 游泳\n" +
                "    <input type=\"checkbox\" name=\"hobby\" value=\"2\"> 爬山 <br>\n" +
                "    <input type=\"submit\">\n" +
                "</form>\n" +
                "</body>\n" +
                "</html>");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
